<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>页面详情</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../../layuiadmin/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="../../../layuiadmin/style/admin.css" media="all">
</head>
<body>
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-form layui-card-header layuiadmin-card-header-auto">
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">根域名</label>
                    <div class="layui-input-block">
                        <input type="text" name="domain" placeholder="请输入" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-inline">
                    <button class="layui-btn" lay-submit lay-filter="LAY-user-front-search">
                        <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
                    </button>
                </div>
            </div>
        </div>

        <div class="layui-card-body">
            <div style="padding-bottom: 10px;">
                <button class="layui-btn layuiadmin-btn-useradmin layui-btn-danger" data-type="batchdel">删除</button>
                <!--<button class="layui-btn layuiadmin-btn-useradmin" data-type="add">添加</button>-->
            </div>

            <table class="layui-hide" id="table_list" lay-filter="table_list"></table>

            <script type="text/html" id="barDemo">
                <a class="layui-btn layui-btn-xs" lay-event="edit">查看</a>
                <a class="layui-btn layui-btn-xs" lay-event="redirect">跳转</a>
                <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
            </script>
        </div>
    </div>
</div>
<script src="../../../layuiadmin/layui/layui.js"></script>

<script>
    layui.config({
        base: '../../../layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'table', 'form'], function(){
        var $ = layui.$;
        var form = layui.form;
        var table = layui.table;
        //监听搜索
        form.on('submit(LAY-user-front-search)', function(data){
            var field = data.field;
            //执行重载
            table.reload('table_list', {
                where: field,
                page: {curr: 1 }
            });
        });

        table.render({
            elem: '#table_list'
            ,url:'/spider/detail/list/'
            ,request: {
                pageName: 'pageNum' //页码的参数名称，默认：page
                ,limitName: 'pageSize' //每页数据量的参数名，默认：limit
            }
            ,parseData: function(res){ //res 即为原始返回的数据
                return {
                    "code": 0, //解析接口状态
                    "msg": res.message, //解析提示文本
                    "count": res.data.total, //解析数据长度
                    "data": res.data.list //解析数据列表
                };
            }
            ,cellMinWidth: 80 //全局定义常规单元格的最小宽度，layui 2.2.1 新增
            ,cols: [[
                {type: "checkbox", fixed: "left"}
                ,{field:'id', title: 'ID', width: 60}
                // ,{field:'domain', title: '根域名', width: 200} //width 支持：数字、百分比和不填写。你还可以通过 minWidth 参数局部定义当前单元格的最小宽度，layui 2.2.1 新增
                // ,{field:'ruleId', title: '规则ID', width: 60}
                ,{field:'title', title: '标题'}
                ,{field:'url', title: '网址'}
                ,{field:'createDate', title: '抓取时间'}
                // ,{field:'isDownload', title: '是否下载图片'}
                // ,{field:'isSuccess', title: '图片是否已下载'}
                // ,{field:'images', title: '图片'}
                // ,{field:'imageNum', title: '图片数量'}
                // ,{field:'date', title: '文章日期'}
                // ,{field:'author', title: '作者'}
                // ,{field:'category', title: '分类'}
                // ,{field:'tags', title: '标签'}
                // ,{field:'content', title: '内容'}
                // ,{field:'description', title: '描述'}
                ,{fixed: 'right', title:'操作', toolbar: '#barDemo', width:180}
            ]]
            ,page: true
        });

        //监听行工具事件
        table.on('tool(table_list)', function(obj){
            var data = obj.data;
            //console.log(obj)
            if(obj.event === 'del'){
                layer.confirm('确定删除吗？', function(index) {
                    $.ajax({
                        url: '/spider/detail/delete',
                        type: 'POST',
                        data: {"ids":data.id},
                        dataType : "json",
                        success: function (result) {
                            if (result.success){
                                table.reload('table_list');
                                layer.msg('已删除');
                            } else {
                                layer.alert("删除失败，原因："+result.message);
                            }
                        }
                    });
                    return false;
                });
            } else if(obj.event === 'redirect'){
                window.open(data.url);
            }else if(obj.event === 'edit'){
                layer.open({
                    type: 2
                    ,title: '编辑'
                    ,content: 'edit.html?id='+data.id
                    ,maxmin: true
                    ,area: ['650px', '650px']
                    ,btn: ['确定', '取消']
                    ,yes: function(index, layero){
                        var iframeWindow = window['layui-layer-iframe'+ index]
                            ,submitID = 'LAY-user-front-submit'
                            ,submit = layero.find('iframe').contents().find('#'+ submitID);

                        //监听提交
                        iframeWindow.layui.form.on('submit('+ submitID +')', function(data){
                            $.ajax({
                                url: '/spider/detail/update',
                                type: 'POST',
                                data: data.field,
                                dataType : "json",
                                success: function (result) {
                                    if (result.success){
                                        layer.msg("编辑成功");
                                        table.reload('table_list'); //数据刷新
                                        layer.close(index); //关闭弹层
                                    } else {
                                        layer.alert("编辑失败，原因："+result.message+",详细原因："+result.details[0].message);
                                    }
                                }
                            });
                            return false;
                        });
                        submit.trigger('click');
                    }
                });
            }
        });

        //事件
        var active = {
            batchdel: function(){
                var checkStatus = table.checkStatus('table_list')
                    ,checkData = checkStatus.data; //得到选中的数据
                if(checkData.length === 0){
                    return layer.msg('请选择数据');
                }
                var checkedId = "";
                for(var i=0;i<checkData.length;i++){//循环数组
                    if (i === 0){
                        checkedId = checkData[i].id;
                    } else {
                        checkedId += ","+checkData[i].id;
                    }
                }
                layer.confirm('确定删除吗？', function() {
                    $.ajax({
                        url: '/spider/detail/delete',
                        type: 'POST',
                        data: {"ids":checkedId},
                        dataType : "json",
                        success: function (result) {
                            if (result.success){
                                table.reload('table_list');
                                layer.msg('已删除');
                            } else {
                                layer.alert("删除失败，原因："+result.message);
                            }
                        }
                    });
                    return false;
                });
            }
            ,add: function(){
                layer.open({
                    type: 2
                    ,title: '添加规则'
                    ,content: 'add.html'
                    ,maxmin: true
                    ,area: ['650px', '650px']
                    ,btn: ['确定', '取消']
                    ,yes: function(index, layero){
                        var iframeWindow = window['layui-layer-iframe'+ index]
                            ,submitID = 'LAY-user-front-submit'
                            ,submit = layero.find('iframe').contents().find('#'+ submitID);

                        //监听提交
                        iframeWindow.layui.form.on('submit('+ submitID +')', function(data){
                            $.ajax({
                                url: '/spider/rule/insert',
                                type: 'POST',
                                data: data.field,
                                dataType : "json",
                                success: function (result) {
                                    if (result.success){
                                        layer.msg("添加成功");
                                        table.reload('rule_list'); //数据刷新
                                        layer.close(index); //关闭弹层
                                    } else {
                                        layer.alert("添加失败，原因："+result.message+",详细原因："+result.details[0].message);
                                    }
                                }
                            });
                            return false;
                        });
                        submit.trigger('click');
                    }
                });
            }
            , edit: function(){
                var checkStatus = table.checkStatus('table_list')
                    ,checkData = checkStatus.data; //得到选中的数据
                if(checkData.length != 1){
                    return layer.msg('只能编辑一条数据');
                }
                layer.open({
                    type: 2
                    ,title: '编辑'
                    ,content: 'edit.html?id='+checkData[0].id
                    ,maxmin: true
                    ,area: ['650px', '650px']
                    ,btn: ['确定', '取消']
                    ,yes: function(index, layero){
                        var iframeWindow = window['layui-layer-iframe'+ index]
                            ,submitID = 'LAY-user-front-submit'
                            ,submit = layero.find('iframe').contents().find('#'+ submitID);

                        //监听提交
                        iframeWindow.layui.form.on('submit('+ submitID +')', function(data){
                            $.ajax({
                                url: '/spider/detail/update',
                                type: 'POST',
                                data: data.field,
                                dataType : "json",
                                success: function (result) {
                                    if (result.success){
                                        layer.msg("编辑成功");
                                        table.reload('table_list'); //数据刷新
                                        layer.close(index); //关闭弹层
                                    } else {
                                        layer.alert("编辑失败，原因："+result.message+",详细原因："+result.details[0].message);
                                    }
                                }
                            });
                            return false;
                        });
                        submit.trigger('click');
                    }
                });
            }
        };
        $('.layui-btn.layuiadmin-btn-useradmin').on('click', function(){
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
            return false;
        });
    });

</script>

</body>
</html>